/*
 * SaveLoad.h
 *
 *  Created on: 2011-03-30
 *      Author: lpieczkowski
 */

#ifndef SAVELOAD_H_
#define SAVELOAD_H_

#include <FApp.h>
#include <FBase.h>
#include <FSystem.h>
#include <FUi.h>
#include <FGraphics.h>
#include <FGraphicsOpengl.h>
#include <FMedia.h>

/**
 * Template for save function. Typenames accepted are:
 * Osp::Base::String& // double // int
 * Returns: bool - save result
 */
template<typename T>
bool Save(Osp::Base::String keyName, const T value)
{
	Osp::App::AppRegistry* appRegistry = Osp::App::Application::GetInstance()->GetAppRegistry();
	T setValue;
	result r = appRegistry->Get(keyName, setValue);

	if ( r == E_KEY_NOT_FOUND)
	{
		result rr = appRegistry->Add(keyName, value);
		if (IsFailed(rr))
			return false;
	}
	else if ( r == E_SUCCESS )
	{
		result rr = appRegistry->Set(keyName, value);
		if (IsFailed(rr))
			return false;
	}

	r = Osp::App::Application::GetInstance()->GetAppRegistry()->Save();
	if ( IsFailed(r))
	{
		AppLog("Failed to save the registry");
		return false;
	}

	return true;
}

/**
 * Template for load function. Typenames accepted are:
 * Osp::Base::String& // double // int
 * Returns: bool - load result
 */
template<typename T>
bool Load(Osp::Base::String keyName, T& outValue)
{
	Osp::App::AppRegistry* appRegistry = Osp::App::Application::GetInstance()->GetAppRegistry();
	result r = appRegistry->Get(keyName, outValue);

	if ( r == E_KEY_NOT_FOUND)
	{
		AppLog("Key not found, no value read");
		return false;
	}
	else if ( r == E_SUCCESS )
	{
		return true;
	}
	else
	{
		return false;
	}
}

#endif /* SAVELOAD_H_ */
